import { SupportMethodType } from '@/types/knife4j'
import { defineStore } from 'pinia'
import { IDocket, ISwaggerResource } from '../knife/types'
import { IDashboardProps } from './types'

const useDashboardStore = defineStore('dashboard', {
  state: (): IDashboardProps => {
    return {
      author: '萧名',
      swagger: '',
      version: 'V1.0.0',
      url: '',
      host: 'https://doc.xiaominfo.com/',
      basePath: '/',
      groupName: '',
      groupUrl: '',
      groupLocation: '',
      description: 'Knife4j是一个集Swagger2 和 OpenAPI3为一体的增强解决方案',
      methodStatistics: [
        { method: 'GET', count: 0 },
        { method: 'POST', count: 0 },
        { method: 'PUT', count: 0 },
        { method: 'DELETE', count: 0 },
        { method: 'PATCH', count: 0 },
        { method: 'OPTIONS', count: 0 },
        { method: 'TRACE', count: 0 },
        { method: 'HEAD', count: 0 },
        { method: 'CONNECT', count: 0 }
      ]
    }
  },
  persist: true,
  actions: {
    initDashboard(data: { resources: ISwaggerResource[]; docket: IDocket }) {
      const {
        docket: {
          basePath,
          host,
          info: { contact, description, version, termsOfService },
          swagger
        }
      } = data
      this.author = contact.name
      this.version = version.toUpperCase().includes('V') ? version : `V ${version}`
      this.swagger = swagger
      this.url = termsOfService
      this.description = description
      this.basePath = basePath
      this.host = host
    },
    updateGroup(groupName: string, groupUrl: string, groupLocation: string) {
      this.groupName = groupName
      this.groupUrl = groupUrl
      this.groupLocation = groupLocation
    },
    updateStatistics(method: SupportMethodType) {
      const find = this.methodStatistics.find((x) => x.method === method)
      find && find.count++
    }
  }
})

export default useDashboardStore
